/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EmoticonPanelChooser.java
 *
 * Created on 21-ago-2011, 10.38.20
 */
package chat;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

/**
 *
 * @author davide
 */
public final class EmoticonPanelChooser extends javax.swing.JPanel {

    private MouseListener mouse;
    private JTextField inputTF;
    private JPopupMenu popMenu;

    /** Creates new form EmoticonPanelChooser */
    public EmoticonPanelChooser(JTextField inputTF, JPopupMenu popMenu) {
        this.inputTF = inputTF;
        this.popMenu = popMenu;
        initMouseListener();
        initComponents();
    }

    protected void initMouseListener() {
        mouse = new MouseListener() {

            public void mouseClicked(MouseEvent e) {
            }

            public void mousePressed(MouseEvent e) {
                if (popMenu.isShowing()) {
                    popMenu.setVisible(false);
                    mouseExited(e);
                }
                String smile = ((JLabel) e.getComponent()).getToolTipText();
                int offset = inputTF.getCaretPosition();
                StringBuffer text = new StringBuffer(inputTF.getText());
                if (smile.equals("smile")) {
                    text.insert(offset, ":)");
                } else if (smile.equals("frown")) {
                    text.insert(offset, ":(");
                } else if (smile.equals("tongue")) {
                    text.insert(offset, ":p");
                } else if (smile.equals("grin")) {
                    text.insert(offset, ":D");
                } else if (smile.equals("gasp")) {
                    text.insert(offset, ":o");
                } else if (smile.equals("wink")) {
                    text.insert(offset, ";)");
                } else if (smile.equals("glasses")) {
                    text.insert(offset, "8)");
                } else if (smile.equals("sunglasses")) {
                    text.insert(offset, "B|");
                } else if (smile.equals("grumpy")) {
                    text.insert(offset, ">:(");
                } else if (smile.equals("unsure")) {
                    text.insert(offset, ":\\");
                } else if (smile.equals("cry")) {
                    text.insert(offset, ":'(");
                } else if (smile.equals("angel")) {
                    text.insert(offset, "O:)");
                } else if (smile.equals("devil")) {
                    text.insert(offset, "3:)");
                } else if (smile.equals("kiss")) {
                    text.insert(offset, ";-*");
                }  else if (smile.equals("heart")) {
                    text.insert(offset, "<3");
                } else if (smile.equals("kiki")) {
                    text.insert(offset, "^_^");
                } else if (smile.equals("confused")) {
                    text.insert(offset, "o.0");
                } else if (smile.equals("curlylips")) {
                    text.insert(offset, ":3");
                } else if (smile.equals("pacman")) {
                    text.insert(offset, ":v");
                } else if (smile.equals("squint")) {
                    text.insert(offset, "-_-");
                }  
                inputTF.setText(text.toString());
            }

            public void mouseReleased(MouseEvent e) {
            }

            public void mouseEntered(MouseEvent e) {
                ((JLabel) e.getComponent()).setBorder(new LineBorder(Color.RED));

            }

            public void mouseExited(MouseEvent e) {
                ((JLabel) e.getComponent()).setBorder(new LineBorder(Color.WHITE));
            }
        };
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        smile = new javax.swing.JLabel();
        tongue = new javax.swing.JLabel();
        frown = new javax.swing.JLabel();
        grin = new javax.swing.JLabel();
        gasp = new javax.swing.JLabel();
        wink = new javax.swing.JLabel();
        glasses = new javax.swing.JLabel();
        sunglasses = new javax.swing.JLabel();
        grumpy = new javax.swing.JLabel();
        unsure = new javax.swing.JLabel();
        cry = new javax.swing.JLabel();
        angel = new javax.swing.JLabel();
        devil = new javax.swing.JLabel();
        confused = new javax.swing.JLabel();
        curlylips = new javax.swing.JLabel();
        heart = new javax.swing.JLabel();
        kiki = new javax.swing.JLabel();
        kiss = new javax.swing.JLabel();
        pacman = new javax.swing.JLabel();
        squint = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(new java.awt.GridLayout(4, 5, 2, 2));

        smile.addMouseListener(mouse);
        smile.setBackground(new java.awt.Color(255, 255, 255));
        smile.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        smile.setIcon(new javax.swing.ImageIcon(System.getProperty("user.dir") + "/chat/emoticon/smile.png"));
        smile.setToolTipText("smile");
        smile.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        add(smile);

        tongue.addMouseListener(mouse);
        tongue.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        tongue.setIcon(new javax.swing.ImageIcon(System.getProperty("user.dir") + "/chat/emoticon/tongue.png"));
        tongue.setToolTipText("tongue");
        tongue.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        add(tongue);

        frown.addMouseListener(mouse);
        frown.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        frown.setIcon(new javax.swing.ImageIcon(System.getProperty("user.dir") + "/chat/emoticon/frown.png"));
        frown.setToolTipText("frown");
        frown.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        add(frown);

        grin.addMouseListener(mouse);
        grin.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        grin.setIcon(new javax.swing.ImageIcon(System.getProperty("user.dir") + "/chat/emoticon/grin.png"));
        grin.setToolTipText("grin");
        grin.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        add(grin);

        gasp.addMouseListener(mouse);
        gasp.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        gasp.setIcon(new javax.swing.ImageIcon(System.getProperty("user.dir") + "/chat/emoticon/gasp.png"));
        gasp.setToolTipText("gasp");
        gasp.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        add(gasp);

        wink.addMouseListener(mouse);
        wink.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        wink.setIcon(new javax.swing.ImageIcon(System.getProperty("user.dir") + "/chat/emoticon/wink.png"));
        wink.setToolTipText("wink");
        wink.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        add(wink);

        glasses.addMouseListener(mouse);
        glasses.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        glasses.setIcon(new javax.swing.ImageIcon(System.getProperty("user.dir") + "/chat/emoticon/glasses.png"));
        glasses.setToolTipText("glasses");
        glasses.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        add(glasses);

        sunglasses.addMouseListener(mouse);
        sunglasses.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        sunglasses.setIcon(new javax.swing.ImageIcon(System.getProperty("user.dir") + "/chat/emoticon/sunglasses.png"));
        sunglasses.setToolTipText("sunglasses");
        sunglasses.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        add(sunglasses);

        grumpy.addMouseListener(mouse);
        grumpy.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        grumpy.setIcon(new javax.swing.ImageIcon(System.getProperty("user.dir") + "/chat/emoticon/grumpy.png"));
        grumpy.setToolTipText("grumpy");
        grumpy.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        add(grumpy);

        unsure.addMouseListener(mouse);
        unsure.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        unsure.setIcon(new javax.swing.ImageIcon(System.getProperty("user.dir") + "/chat/emoticon/unsure.png"));
        unsure.setToolTipText("unsure");
        unsure.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        add(unsure);

        cry.addMouseListener(mouse);
        cry.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        cry.setIcon(new javax.swing.ImageIcon(System.getProperty("user.dir") + "/chat/emoticon/cry.png"));
        cry.setToolTipText("cry");
        cry.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        add(cry);

        angel.addMouseListener(mouse);
        angel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        angel.setIcon(new javax.swing.ImageIcon(System.getProperty("user.dir") + "/chat/emoticon/angel.png"));
        angel.setToolTipText("angel");
        angel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        add(angel);

        devil.addMouseListener(mouse);
        devil.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        devil.setIcon(new javax.swing.ImageIcon(System.getProperty("user.dir") + "/chat/emoticon/devil.png"));
        devil.setToolTipText("devil");
        devil.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        add(devil);

        confused.addMouseListener(mouse);
        confused.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        confused.setIcon(new javax.swing.ImageIcon(System.getProperty("user.dir") + "/chat/emoticon/confused.png"));
        confused.setToolTipText("confused");
        confused.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        add(confused);

        curlylips.addMouseListener(mouse);
        curlylips.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        curlylips.setIcon(new javax.swing.ImageIcon(System.getProperty("user.dir") + "/chat/emoticon/curlylips.png"));
        curlylips.setToolTipText("curlylips");
        curlylips.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        add(curlylips);

        heart.addMouseListener(mouse);
        heart.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        heart.setIcon(new javax.swing.ImageIcon(System.getProperty("user.dir") + "/chat/emoticon/heart.png"));
        heart.setToolTipText("heart");
        heart.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        add(heart);

        kiki.addMouseListener(mouse);
        kiki.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        kiki.setIcon(new javax.swing.ImageIcon(System.getProperty("user.dir") + "/chat/emoticon/kiki.png"));
        kiki.setToolTipText("kiki");
        kiki.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        add(kiki);

        kiss.addMouseListener(mouse);
        kiss.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        kiss.setIcon(new javax.swing.ImageIcon(System.getProperty("user.dir") + "/chat/emoticon/kiss.png"));
        kiss.setToolTipText("kiss");
        kiss.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        add(kiss);

        pacman.addMouseListener(mouse);
        pacman.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        pacman.setIcon(new javax.swing.ImageIcon(System.getProperty("user.dir") + "/chat/emoticon/pacman.png"));
        pacman.setToolTipText("pacman");
        pacman.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        add(pacman);

        squint.addMouseListener(mouse);
        squint.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        squint.setIcon(new javax.swing.ImageIcon(System.getProperty("user.dir") + "/chat/emoticon/squint.png"));
        squint.setToolTipText("squint");
        squint.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        add(squint);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel angel;
    private javax.swing.JLabel confused;
    private javax.swing.JLabel cry;
    private javax.swing.JLabel curlylips;
    private javax.swing.JLabel devil;
    private javax.swing.JLabel frown;
    private javax.swing.JLabel gasp;
    private javax.swing.JLabel glasses;
    private javax.swing.JLabel grin;
    private javax.swing.JLabel grumpy;
    private javax.swing.JLabel heart;
    private javax.swing.JLabel kiki;
    private javax.swing.JLabel kiss;
    private javax.swing.JLabel pacman;
    private javax.swing.JLabel smile;
    private javax.swing.JLabel squint;
    private javax.swing.JLabel sunglasses;
    private javax.swing.JLabel tongue;
    private javax.swing.JLabel unsure;
    private javax.swing.JLabel wink;
    // End of variables declaration//GEN-END:variables
}
